package com.android.tea;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.tea.config.Config;
import com.android.tea.util.MyWebViewClient;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.widght.TeaWebView;

public class ShopActivity extends Activity implements OnClickListener{
	private static final String TAG = "ShopActivity";

	private TextView tab_allfriends;
	private TextView tab_contacts;
	private TextView tab_chart;
	private TextView tab_select;
	private ProgressBar progressBar;
	private TeaWebView webView;
	private String initUrl ;
	private String currentUrl = null;
	
	private String All = "all";
	private String Search = "search";
	private String Category = "sategory";
	private String Car = "car";
	private final String Flow = "Flow";

	private LinearLayout topLayout;

	private ImageView img_logo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop);
		TeaEncyclopediaApp.list.add(this);
		initLayout();
		initUrl = Config.SHOP_URL;
		
		// TeaLog.e(TAG, "initUrl is " + initUrl);
		if (StringUtils.isNotNullOrEmpty(initUrl )
				&& URLUtil.isNetworkUrl(initUrl)) {
			webView.loadUrl(initUrl);
		}
		
	}
	
	private void initLayout() {
		topLayout = (LinearLayout)findViewById(R.id.top_layout);
		img_logo = (ImageView)findViewById(R.id.img_logo);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		Options opts = new Options();
		opts.inJustDecodeBounds = true;
		int tabH = (int) (opts.outHeight
				* getResources().getDisplayMetrics().density / 2);
		tab_allfriends = (TextView) findViewById(R.id.tab_allfriends);
		tab_contacts = (TextView) findViewById(R.id.tab_contact);
		tab_chart = (TextView) findViewById(R.id.tab_chart);
		tab_select = (TextView) findViewById(R.id.tab_select);
		tab_allfriends.setHeight(tabH);
		tab_contacts.setHeight(tabH);
		tab_chart.setHeight(tabH);
		
		tab_allfriends.setBackgroundResource(R.drawable.shop_item_bg);
		tab_allfriends.setTextColor(getResources().getColor(R.color.green));
		
		tab_allfriends.setOnClickListener(this);
		tab_contacts.setOnClickListener(this);
		tab_chart.setOnClickListener(this);
		tab_select.setOnClickListener(this);
		
		
		initWebView();
		initCache();
	}

	
	private void initCache() {
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

	}

	private void initWebView() {
		webView = (TeaWebView) findViewById(R.id.webView);
		webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setBlockNetworkImage(true);
		webView.setWebChromeClient(webChromeClient);
		webView.setWebViewClient(webViewClient);
		webView.setHorizontalScrollBarEnabled(false);// 水平不显示
		webView.setVerticalScrollBarEnabled(false); // 垂直不显示
	}
	
	WebViewClient webViewClient = new MyWebViewClient() {

		@Override
		public void doUpdateVisitedHistory(WebView view, String url,
				boolean isReload) {
			TeaLog.e(TAG, "doUpdateVisitedHistory url is " + url);
			if (progressBar != null)
				progressBar.setVisibility(View.GONE);
			super.doUpdateVisitedHistory(view, url, isReload);
		}

		@Override
		public void onLoadResource(WebView view, String url) {
			super.onLoadResource(view, url);
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onScaleChanged(WebView view, float oldScale, float newScale) {
			super.onScaleChanged(view, oldScale, newScale);
		}

		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			changeTitle(url);
			
			super.onPageStarted(view, url, favicon);

		};


		public void onPageFinished(WebView view, String url) {
			Log.e(TAG, "onPageFinished url is " + url);
			webView.getSettings().setBlockNetworkImage(false);
//			int indexOf = url.indexOf("?");
			// if(-1!=indexOf) {
			// currentUrl = url.substring(0, indexOf);
			// }else {
			currentUrl = url;
			// }

			// view.loadUrl(url);
		};

	};

	WebChromeClient webChromeClient = new WebChromeClient() {

		public void onProgressChanged(WebView view, int progress) {
			super.onProgressChanged(view, progress);
			if (view.getUrl() == null
					|| "about:blank".equalsIgnoreCase(view.getUrl())) {
				progressBar.setVisibility(View.VISIBLE);
				progressBar.setProgress(10);
				return;
			}
			progressBar.setMax(100);
			if (progress < 100) {
				progressBar.setVisibility(View.VISIBLE);
				if (progress < 10) {
					progressBar.setProgress(10);
				} else {
					progressBar.setProgress(progress);
				}
			} else {
				progressBar.setProgress(100);
				progressBar.setVisibility(View.GONE);
			}
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
		}

	};

	private void changeTitle(String url) {
		String loadUrl = null;
		int indexOf = url.indexOf("?");
		if (-1 != indexOf) {
			loadUrl = url.substring(0, indexOf);
		} else {
			loadUrl = url;
		}
		Log.e(TAG, "loadurl is " + loadUrl);
		if(loadUrl.endsWith("index.php")||loadUrl.endsWith("goods.php")||loadUrl.endsWith("category.php")||loadUrl.equals(Config.SHOP_URL)) {
			onTabChange(All);
		}else if(loadUrl.endsWith("search.php")) {
			onTabChange(Search);
		}else if(loadUrl.endsWith("cat_all.php")||loadUrl.endsWith("cat.php")) {
			onTabChange(Category);
		}else if(loadUrl.endsWith("cart.php")){
			onTabChange(Car);
		}else if(loadUrl.endsWith("flow.php")||loadUrl.endsWith("user.php")) {
			onTabChange(Flow);
		}
	}
	
	public void onTabChange(String tabId) {
		tab_allfriends.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_contacts.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_chart.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_select.setBackgroundColor(getResources().getColor(R.color.bg_green));

		tab_allfriends.setTextColor(getResources()
				.getColor(R.color.tab_default));
		tab_contacts.setTextColor(getResources().getColor(R.color.tab_default));
		tab_chart.setTextColor(getResources().getColor(R.color.tab_default));
		tab_select.setTextColor(getResources().getColor(R.color.tab_default));

		topLayout.setVisibility(View.VISIBLE);
		img_logo.setVisibility(View.GONE);
		if (All.equalsIgnoreCase(tabId)) {
			tab_allfriends.setBackgroundResource(R.drawable.shop_item_bg);
			tab_allfriends.setTextColor(getResources().getColor(R.color.green));
		} else if (Search.equalsIgnoreCase(tabId)) {
			tab_contacts.setBackgroundResource(R.drawable.shop_item_bg);
			tab_contacts.setTextColor(getResources().getColor(R.color.green));
		} else if (Category.equalsIgnoreCase(tabId)) {
			tab_chart.setBackgroundResource(R.drawable.shop_item_bg);
			tab_chart.setTextColor(getResources().getColor(R.color.green));
		} else if (Car.equalsIgnoreCase(tabId)) {
			tab_select.setBackgroundResource(R.drawable.shop_item_bg);
			tab_select.setTextColor(getResources().getColor(R.color.green));
		}else if(Flow.equalsIgnoreCase(tabId)) {
			topLayout.setVisibility(View.GONE);
			img_logo.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	protected void onStop() {
		TeaLog.e(TAG, "stopLoading is running");
		webView.stopLoading();
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		tab_allfriends.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_contacts.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_chart.setBackgroundColor(getResources().getColor(R.color.bg_green));
		tab_select.setBackgroundColor(getResources().getColor(R.color.bg_green));

		tab_allfriends.setTextColor(getResources().getColor(R.color.tab_default));
		tab_contacts.setTextColor(getResources().getColor(R.color.tab_default));
		tab_chart.setTextColor(getResources().getColor(R.color.tab_default));
		tab_select.setTextColor(getResources().getColor(R.color.tab_default));
		
		webView.stopLoading();
		switch (v.getId()) {
		case R.id.tab_allfriends:
			tab_allfriends.setBackgroundResource(R.drawable.shop_item_bg);
			tab_allfriends.setTextColor(getResources().getColor(R.color.green));
			
			webView.loadUrl(Config.SHOP_URL);
			
			break;
		case R.id.tab_contact:
			tab_contacts.setBackgroundResource(R.drawable.shop_item_bg);
			tab_contacts.setTextColor(getResources().getColor(R.color.green));
			
			webView.loadUrl(Config.SHOP_SEARCH_URL);
			break;
		case R.id.tab_chart:
			tab_chart.setBackgroundResource(R.drawable.shop_item_bg);
			tab_chart.setTextColor(getResources().getColor(R.color.green));
			
			webView.loadUrl(Config.SHOP_CATEGORY_URL);
			break;
		case R.id.tab_select:
			tab_select.setBackgroundResource(R.drawable.shop_item_bg);
			tab_select.setTextColor(getResources().getColor(R.color.green));
			
			webView.loadUrl(Config.SHOP_CAR_URL);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		if (Config.SHOP_URL.equals(currentUrl)||Config.SHOP_SEARCH_URL.equals(currentUrl)||Config.SHOP_CATEGORY_URL.equals(currentUrl)||Config.SHOP_CAR_URL.equals(currentUrl)) {
			// 退出程序
			Activity parent = this.getParent();
			if (parent != null) {
				parent.onBackPressed();
			} else {
				super.onBackPressed();
			}
		} else {
			webView.goBack();
		}
	}
}
